<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('出资信息') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input v-model="queryParams.dealName" :placeholder="$t('项目名称')" clearable />
      </el-form-item>
      <el-form-item :label="$t('付款类型')" prop="paymentType">
        <el-select v-model="queryParams.paymentType" :placeholder="$t('付款类型')" clearable>
          <el-option
            v-for="dict in paymentTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:investinfo:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="investInfoList" :show-summary="true" :summary-method="getSummaries" border :span-method="objectSpanMethod" class="noneHoverBg">
      <el-table-column :label="$t('项目名称')" show-overflow-tooltip fixed align="center" prop="dealName" width="350">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.dealId,companyId:scope.row.companyId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目负责人')" fixed align="center" prop="personChargeName" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="dealPaymentDetailHandle(scope.row)">{{ scope.row.personChargeName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('协议金额') + $t('金额单位')" header-align="center" align="right" prop="agreementMoney" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">
            {{ moneyFormat(scope.row.agreementMoney, 'w') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际付款总额') + $t('金额单位')" header-align="center" align="right" prop="totalAmount" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('付款类型')" align="center" prop="firstTransferTime" :formatter="paymentTypeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('付款日期')" align="center" prop="paymentDate" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际付款金额') + $t('金额单位')" header-align="center" align="right" prop="paymentMoney" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.paymentMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>
            <el-button
              size="small"
              type="text"
              icon="el-icon-s-order"
              style="margin-left: 5px;"
              @click="dealPaymentDetailHandle(scope.row)"
            >{{ $t('划款分摊明细') }}
            </el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 项目划款明细列表 -->
    <el-dialog :title="$t(listTitle)" :visible.sync="listOpen" width="70%" :close-on-click-modal="false" append-to-body custom-class="dialogForm">
      <dealPaymentDetailList ref="dealPaymentDetailList" />
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getFundInvestInfo, getFundInvestInfoSum } from '@/api/fund/fundBase'
import { fdSubjectSelectByType } from '@/api/fd/config/fdSubjects'
import dealPaymentDetailList from '@/views/deal/paymentDetail/dealPaymentDetailList'
export default {
  components: {
    dealPaymentDetailList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      listTitle: '',
      listOpen: false,
      // 出资信息表格数据
      investInfoList: [],
      investInfoSum: {},
      // 首次划款时间
      firstTransferTimes: undefined,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      dateRange: undefined,
      // 付款类型
      paymentTypeOptions: [],
      // 查询参数
      queryParams: {
        dealName: undefined,
        paymentType: undefined,
        fundId: this.$route.query.id,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      currencyOptions: []
    }
  },
  created() {
    // 付款类型
    fdSubjectSelectByType('deal', 'oa_payment').then(response => {
      this.paymentTypeOptions = response.data
    })
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询项目现金流列表 */
    getList() {
      this.loading = true
      getFundInvestInfoSum(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.investInfoSum = response.data
      })
      getFundInvestInfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.investInfoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    formatCurrency(val) {
      const currency = this.currencyOptions.find(obj => obj.dictValue === val)
      if (currency) {
        return currency.dictLabel
      }
      return ''
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = undefined
      this.queryHandle()
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/base/fundBase/getFundInvestInfo/export', this.queryParams)
      }).catch(() => {
      })
    },
    /** 项目划款明细 */
    dealPaymentDetailHandle(row) {
      this.listOpen = true
      this.listTitle = this.$t('划款分摊明细')
      this.$nextTick(() => {
        // 调用List页面的 dealPaymentDetailList 方法初始化数据
        this.$refs.dealPaymentDetailList.getDealPaymentDetail(row)
      })
    },
    // 付款类型翻译
    paymentTypeFormat(row) {
      return this.selectDictLabel(this.paymentTypeOptions, row.paymentType)
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex <= 4) {
        const spanArr = this.rowspan()
        for (let i = 0; i < spanArr.length; i++) {
          const _row = spanArr[rowIndex]
          return {
            rowspan: _row,
            colspan: 1
          }
        }
      }
    },
    rowspan() {
      // 二维数组，用于存放单元格合并规则
      const spanArr = []
      // 用于存储相同项的开始index
      let position = 0
      this.investInfoList.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1)
          position = 0
        } else {
          if (this.investInfoList[index].dealId === this.investInfoList[index - 1].dealId) {
            spanArr[position] += 1 // 有相同项
            spanArr.push(0) // 名称相同后往数组里面加一项0
          } else {
            spanArr.push(1) // 同列的前后两行单元格不相同
            position = index
          }
        }
      })
      return spanArr
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 2) {
          sums[index] = this.moneyFormat(this.investInfoSum.agreementMoney, 'w')
        } else if (index === 3) {
          sums[index] = this.moneyFormat(this.investInfoSum.totalAmount, 'w')
        } else if (index === 6) {
          sums[index] = this.moneyFormat(this.investInfoSum.paymentMoney, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
